var RequestForm = Base.extend({

    AJAX_HANDLER_URL: '/form_submit_ajax_handler.php',
    VALIDATION_MESSAGES: {
        name_required: "Введите, пожалуйста, Ваше имя.",
        email_required: "Пожалуйста, укажите Email.",
        incorrect_email: "Некорректный адрес электронной почты.",
        phone_required: "Пожалуйста, укажите Ваш телефон. Пример:&nbsp;+7&nbsp;(917)&nbsp;765&nbsp;2312"
    },

    _defaultTextInputValues: {},
    _formTextInputs: null,
    _formContainer: null,

    constructor: function(form)
    {
        this._formContainer = $(form);
        this._formTextInputs = this._formContainer.find('input[type="text"],textarea');
        this._defaultTextInputValues = this._getDefaultTextInputValues();
        this._formTextInputs.bind('focusin', handler(this, '_onTextInputFocusIn'));
        this._formTextInputs.bind('focusout', handler(this, '_onTextInputFocusOut'));
        this._initValidation();
    },

    _initValidation: function()
    {
        var thisPtr = this;
        $.validator.addMethod('required_not_default', function(value, element){
            return (value != '') && (!thisPtr._hasDefaultValue(element));
        });
        this._formContainer.validate( this._prepareValidatorSettings() );
    },

    _onSubmit: function(evt)
    {
        var submitParams = this._getFormSubmitParams();
        $.ajax({
            type: 'post',
            url: this.AJAX_HANDLER_URL,
            data: submitParams,
            success: handler(this, '_onAjaxSuccess')
        });
        return false;
    },


    _onAjaxSuccess: function(resp)
    {
        if ( resp == '' )
        {
            alert( "Ваша заявка успешно отправлена!" );
        }
        else
        {
            alert( resp );
        }
    },

    _hasDefaultValue: function(el)
    {
        el = $(el);
        return el.val() == this._defaultTextInputValues[el.attr('name')];
    },

    _onTextInputFocusIn: function(evt)
    {
        var curEl = $(evt.currentTarget);
        if (this._hasDefaultValue(curEl))
        {
            curEl.val('');
        }
    },

    _onTextInputFocusOut: function(evt)
    {
        var curEl = $(evt.currentTarget);
        var curElName = curEl.attr('name');
        if (curEl.val() == '')
        {
            curEl.val(this._defaultTextInputValues[curElName]);
        }
    },

    _getFormSubmitParams: function()
    {
        var thisPtr = this;
        var formSubmitParams = {};
        this._formTextInputs.each(function() {
            var curEl = $(this);
            var textInputName = curEl.attr('name');
            formSubmitParams[textInputName] = (!thisPtr._hasDefaultValue(curEl)) ? curEl.val() : '';
        });
        this._formContainer.find('input[type="hidden"],select').each(function(){
            var curEl = $(this);
            formSubmitParams[curEl.attr('name')] = curEl.val();
        });
        this._formContainer.find('input[type="checkbox"]').each(function(){
            var curEl = $(this);
            formSubmitParams[curEl.attr('name')] = (curEl.is(':checked')) ? curEl.val() : '';
        });

        return formSubmitParams;
    },

    _getDefaultTextInputValues: function()
    {
        var textInputValues = {};
        this._formTextInputs.each(function() {
            var curEl = $(this);
            var textInputName = curEl.attr('name');
            textInputValues[textInputName] = curEl.prop('defaultValue');
        });
        return textInputValues;
    },

    _prepareValidatorSettings: function()
    {
        return {
            onfocusout: false,
            focusInvalid: false,
            focusCleanup: true,
            onkeyup: false,
            submitHandler: handler( this, '_onSubmit' ),
            highlight: handler(this, '_highlightError'),
            unhighlight: handler(this, '_unHighlightError'),
            rules:
            {
                form_text_1:
                {
                    required: true,
                    required_not_default: true
                },
                form_text_3:
                {
                    required_not_default: true
                },
                form_email_2:
                {
                    required_not_default: true,
                    email: true
                }
            },

            messages:
            {
                form_text_1:
                {
                    required_not_default: this.VALIDATION_MESSAGES.name_required
                },
                form_email_2:
                {
                    email:                this.VALIDATION_MESSAGES.incorrect_email,
                    required_not_default: this.VALIDATION_MESSAGES.email_required
                },
                form_text_3:
                {
                    required_not_default: this.VALIDATION_MESSAGES.phone_required
                }
            }
        };
    },

    _highlightError: function(element, errorClass)
    {
        var el = $(element);
        el.parent().addClass(errorClass);
        el.addClass(errorClass);
    },

    _unHighlightError: function(element, errorClass)
    {
        var el = $(element);
        el.parent().removeClass(errorClass);
        el.removeClass(errorClass);
    }

});